/*
 * @FileName: [RechargeCardTradeServiceImpl.java] 
 * @Package com.bcinfo.iccard.service.impl 
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com.
 * All rights reserved.
 * 
 * This software is the confidential and proprietary 
 * information of BCINFO Technology Limited Company
 * ("Confidential Information"). You shall not disclose 
 * such Confidential Information and shall use it only
 * in accordance with the terms of the contract agreement 
 * you entered into with RKY.
 * 
 * $Rev: 566 $
 * $LastChangedDate: 2013-12-18 15:05:16 +0800 (Wed, 18 Dec 2013) $
 * $LastChangedBy: wanggc $
 * 
 * @category ICCARD
 * @version 1.1
 * @author $Author: wanggc $
 *
 * Change History:[Formatter: author date description] <br/>
 * 1
 * 2
 * 3
*/


package com.bcinfo.iccard.service.impl;

import java.text.MessageFormat;
import java.util.Date;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import com.bcinfo.iccard.dao.RechargeableCardMapper;
import com.bcinfo.iccard.dao.RechargeablecardExpensiveRecordMapper;
import com.bcinfo.iccard.dao.RechargeablecardExpensiveRecordTempMapper;
import com.bcinfo.iccard.exception.IccardDatabaseException;
import com.bcinfo.iccard.model.RechargeablecardExpensiveRecord;
import com.bcinfo.iccard.model.RechargeablecardExpensiveRecordTemp;
import com.bcinfo.iccard.service.RechargeCardTradeService;

/**
 * 充值卡交易服务 <br/>
 * <p>Description:<br/>
 *    <p>TODO 
 *
 * <p>For Examples <br/>
 * <PRE>
 * </PRE>
 * <p>
 */
@Service
public class RechargeCardTradeServiceImpl implements RechargeCardTradeService
{
    
    private static Logger _logger=Logger.getLogger(RechargeCardTradeServiceImpl.class);
    @Autowired
    private RechargeablecardExpensiveRecordTempMapper rechargeablecardExpensiveRecordTempMapper;
    @Autowired
    private RechargeablecardExpensiveRecordMapper rechargeablecardExpensiveRecordMapper;
    @Autowired
    private RechargeableCardMapper rechargeableCardMapper;
    
    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @param merchantCode
     * @param posCode
     * @param serialNum
     * @return
     * @throws IccardDatabaseException 
     * @see com.bcinfo.iccard.service.RechargeCardTradeService#feedBackFailedTradeByMerPos(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public void feedBackFailedTradeByMerPos(String merchantCode, String posCode,
            String serialNum) throws IccardDatabaseException
    {
        
        if(_logger.isDebugEnabled()){
            _logger.debug(MessageFormat.format("feedBackFailedTradeByMerPos:merchantCode={0},posCode={1},serialNum={2}", merchantCode, posCode, serialNum));
        }
        try
        {
            RechargeablecardExpensiveRecordTemp temp=rechargeablecardExpensiveRecordTempMapper.loadBySerialNum(serialNum);
            //存在记录且状态不为异常状态和撤销状态
            if(temp!=null){
                
                if(temp.getExpensiveStatus()==1){
                    //已正式消费
                    //未消费成功的记录金额返还
                    rechargeablecardExpensiveRecordTempMapper.feedBackMoney(merchantCode, posCode, temp.getRechargeablecardExpensiveTempId());
                    //将未消费成功的记录改为异常状态
                    rechargeablecardExpensiveRecordTempMapper.setExceptionStatus(merchantCode, posCode, temp.getRechargeablecardExpensiveTempId());
                }else if(temp.getExpensiveStatus()==0){
                    //预消费    
                    temp.setExpensiveStatus(1);
                    temp.setUpdateTime(new Date());
                    //更新成已消费
                    rechargeablecardExpensiveRecordTempMapper.updateByPrimaryKeySelective(temp);
                    //将该预消费插入到已消费表
                    RechargeablecardExpensiveRecord record=new RechargeablecardExpensiveRecord();
                    record.setExpensiveDate(new Date());
                    record.setOpUserId(1L);
                    record.setRechargeablecardExpensiveTempId(temp.getRechargeablecardExpensiveTempId());
                    rechargeablecardExpensiveRecordMapper.insertSelective(record);
                }
            }
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
        
    }

    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @param temp
     * @return
     * @throws IccardDatabaseException 
     * @see com.bcinfo.iccard.service.RechargeCardTradeService#addTempExpensive(com.bcinfo.iccard.model.RechargeablecardExpensiveRecordTemp)
     */
    @Override
    public RechargeablecardExpensiveRecordTemp addTempExpensive(RechargeablecardExpensiveRecordTemp temp)
            throws IccardDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug(MessageFormat.format("addTempExpensive:temp=={0}",temp.toString()));
        }
        try
        {
            //插入临时表
            rechargeablecardExpensiveRecordTempMapper.insertSelective(temp);
            //扣钱
            rechargeableCardMapper.consume(temp.getRechargeablecardCode(), temp.getExpenseAmount());
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
        return temp;
    }

    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @param temp
     * @return
     * @throws IccardDatabaseException 
     * @see com.bcinfo.iccard.service.RechargeCardTradeService#update(com.bcinfo.iccard.model.RechargeablecardExpensiveRecordTemp)
     */
    @Override
    public int update(RechargeablecardExpensiveRecordTemp temp) throws IccardDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug("update:temp="+temp.toString());
        }
        try
        {
            int rows=rechargeablecardExpensiveRecordTempMapper.updateByPrimaryKeySelective(temp);
            return rows;
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
    }

    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @param serialNum
     * @return
     * @throws IccardDatabaseException 
     * @see com.bcinfo.iccard.service.RechargeCardTradeService#loadBySerialNum(java.lang.String)
     */
    @Override
    public RechargeablecardExpensiveRecordTemp loadBySerialNum(String serialNum) throws IccardDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug("loadBySerialNum:serialNum="+serialNum);
        }
        try
        {
            RechargeablecardExpensiveRecordTemp temp=rechargeablecardExpensiveRecordTempMapper.loadBySerialNum(serialNum);
            return temp;
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
    }

    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @param serialNum
     * @throws IccardDatabaseException 
     * @see com.bcinfo.iccard.service.RechargeCardTradeService#revocate(java.lang.String)
     */
    @Override
    public void revocate(RechargeablecardExpensiveRecordTemp temp) throws IccardDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug("revocate:serialNum="+temp.toString());
        }
        try
        {
            //拨回用户款
            rechargeablecardExpensiveRecordTempMapper.revocate(temp.getSerialNumber());
            //更新为撤销状态，同时更新撤销时间
            temp.setExpensiveStatus(4);
            rechargeablecardExpensiveRecordTempMapper.updateByPrimaryKeySelective(temp);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
    }

}
